<?php
namespace adapters;

abstract class debug extends \patterns\Adapter /*implements \interfaces\debug*/
{
	private $message = "";
	private $title = "";
	private $backtrace = array();

	protected $components = array();

	public function TimerStart($name = "")
	{
		if ($name == "startup")
		{
			service("debug")->error("Timer name cannot be '$name'");
		}

		core("Registry")->Set($name, microtime(1), 'timer');
	}

	public function LogComponent($name, $time)
	{
		$this->components[] = array($name, $time);
		return true;
	}

	public function TimerStop($name = "")
	{
		$end = microtime(1);

		$start = core("Registry")->Get($name, 'timer');

		$res = round($end - $start, 3);

		return $res;
	}

	public function TimerCheckpoint()
	{
		return $this->TimerStop("startup");
	}

	public function SetMessage($message)
	{
		$this->message = (string)$message;
		return $this;
	}

	protected function GetMessage()
	{
		return $this->message;
	}

	public function SetTitle($title)
	{
		$this->title = (string)$title;
		return $this;
	}

	protected function GetTitle()
	{
		return $this->title;
	}

	protected function GetBacktrace()
	{
		$debug = debug_backtrace();

		$res = array();

		foreach ($debug as $item)
		{
			!isset($item['file']) && $item['file'] = "";
			!isset($item['line']) && $item['line'] = "";
			!isset($item['class']) && $item['class'] = "";
			!isset($item['type']) && $item['type'] = "";

			$item['file'] = basename($item['file']);

			$args = array();
			foreach ($item['args'] as $arg)
			{
				if (is_string($arg))
				{
					$arg = "'$arg'";
				}
				elseif(!is_numeric($arg))
				{
					$arg = gettype($arg);
				}

				$args[] = $arg;
			}

			$args = implode(", ", $args);

			$res[] = array('file'=>$item['file'], 'line'=>$item['line'], 'class'=>$item['class'], 'type'=>$item['type'], 'function'=>$item['function'], 'args'=>$args);
		}

		return $res;
	}

	//Those must be implemented
	public abstract function DbStats();
	public abstract function GenericStats();
	public abstract function ComponentsStats();
	public abstract function ServicesStats();
}